package cn.lhx.blog.action;

import java.util.List;

import javax.annotation.Resource;
import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.lhx.blog.pojo.User;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.AdminFormBean;
import cn.lhx.blog.pojo.ArticleCategory;
import cn.lhx.blog.service.ArtiCatgService;
import cn.lhx.blog.service.ArticleOrgService;
import cn.lhx.blog.service.ArticleService;
import cn.lhx.blog.service.UserService;
import cn.lhx.blog.util.BlogInfo;
/**
 * 执行管理员动作
 * @author lhx
 *
 */
public class AdminAction extends DispatchAction{
	@Resource
	private ArtiCatgService artiCatgService;
	@Resource
	private ArticleOrgService articleOrgService;
	@Resource
	private ArticleService articleService;
	@Resource
	private UserService userService;
	
	/**
	 * 初始化后台管理多有分类、来源信息
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward initAdmin(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
		//开始获取分类信息
		log.info("后台初始化,即将进入后台管理");
		List<ArticleCategory> artCatgList=artiCatgService.getAllCategory();
		request.setAttribute("categories", artCatgList);
		return mapping.findForward("admin_page");
	}
	/**
	 * 后台登陆
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.info("管理员登陆");
		boolean isPass=false;
		AdminFormBean adminf=(AdminFormBean)form;
		String name=adminf.getName();
		String password=adminf.getPassword();
		
		User admin=new User();
		admin.setName(name);
		admin.setPassword(password);
		if(BlogInfo.ADMIN_NAME.equals(admin.getName())){
			admin=userService.hasUser(admin);
			if(null!=admin){
				request.getSession().setAttribute("adminIsLogin", admin);
				isPass=true;
			}
		}		
		
		if(isPass){
			log.info("验证通过");
			return mapping.findForward("loginOK");
		}else{
			log.info("验证失败");
			return mapping.findForward("loginFail");
		}
		
		
		
//		req.getRequestDispatcher("admin.do?method=initAdmin").forward(req,resp);
//		return null;
	}
	
	/**
	 * 管理员退出
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward logout(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
		log.info("管理员退出");
		request.getSession().setAttribute("adminIsLogin", null);
		return mapping.findForward("logout_page");
	}
	

}
